/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLLER_ADMIN;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
 *
 * @author samph
 */
public class GhiNhanCauHoi {
    static public int ThemCauHoi(ArrayList<CauHoi> arr, String path) throws ParserConfigurationException, TransformerConfigurationException, TransformerException
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Danhsach");
        doc.appendChild(rootElement);
        
        for(int k = 0 ; k < arr.size(); k++)
        {
            CauHoi ch = arr.get(k);
            Element Cauhoi = doc.createElement("Cauhoi");
            rootElement.appendChild(Cauhoi);

            
            Element idcauhoi = doc.createElement("Idcauhoi");
            idcauhoi.appendChild(doc.createTextNode(Integer.toString(ch.getIdcauhoi())));
            Cauhoi.appendChild(idcauhoi);

            //String s ="sdfs";
            Element noidung = doc.createElement("Noidung");
            noidung.appendChild(doc.createTextNode(ch.getQuestion()));
            Cauhoi.appendChild(noidung);


            for(int i = 0 ; i < 4; i++)
            {
                Element traloi = doc.createElement("Luachon");
                String str_traloi = "";
                switch (i)
                {
                    case 0:
                        str_traloi = ch.getAnswer_1();
                        break;
                    case 1:
                        str_traloi = ch.getAnswer_2();
                        break;
                    case 2:
                        str_traloi = ch.getAnswer_3();
                        break;
                    case 3:
                        str_traloi = ch.getAnswer_4();
                        break;
                }
                traloi.appendChild(doc.createTextNode(str_traloi));
                Cauhoi.appendChild(traloi);
            }

            Element dapan = doc.createElement("Dapan");
            dapan.appendChild(doc.createTextNode(Integer.toString(ch.getResult())));
            Cauhoi.appendChild(dapan);

            Element dokho = doc.createElement("Dokho");
            dokho.appendChild(doc.createTextNode(Integer.toString(ch.getLevel())));
            Cauhoi.appendChild(dokho);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(path));
        transformer.transform(source, result);

        return 1;
    }
}
